package com.ilop.sthome.mvp.present.mine;

import android.content.Context;

import com.example.common.mvp.BasePresenterImpl;
import com.ilop.sthome.data.bean.AssessBean;
import com.ilop.sthome.data.enums.CellsEnum;
import com.ilop.sthome.data.enums.ProductGroup;
import com.ilop.sthome.mvp.contract.mine.AssessContract;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.siterwell.familywellplus.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 2019-10-10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class AssessPresenter extends BasePresenterImpl<AssessContract.IView> implements AssessContract.IPresent {

    private Context mContext;
    private float mFraction = 0.0f;

    private List<AssessBean.DeviceBen> deviceList1;
    private List<AssessBean.DeviceBen> deviceList2;
    private List<AssessBean.DeviceBen> deviceList3;
    private List<AssessBean.DeviceBen> deviceList4;


    public AssessPresenter(Context mContext) {
        this.mContext = mContext;
        deviceList1 = new ArrayList<>();
        deviceList2 = new ArrayList<>();
        deviceList3 = new ArrayList<>();
        deviceList4 = new ArrayList<>();
    }

    @Override
    public void getDeviceInstallInfo() {
        for (ProductGroup group: ProductGroup.values()) {
            if (group.getDeviceType() == CellsEnum.SMART_DEVICE_GATEWAY){
                AssessBean.DeviceBen device = new AssessBean.DeviceBen();
                device.setDeviceIcon(group.getDrawableResId());
                device.setDeviceType(group.getTypeStrId());
                int install = DeviceDaoUtil.getInstance().findAllGateway().size();
                float fraction = install > 0 ? group.getFraction() : 0.0f;
                mFraction = mFraction + fraction + 10f; //系统10分
                device.setInstall(install);
                device.setFraction(fraction);
                deviceList1.add(device);
            }else if (group.getDeviceType() == CellsEnum.SMART_DEVICE_FIRE){
                AssessBean.DeviceBen device = new AssessBean.DeviceBen();
                device.setDeviceIcon(group.getDrawableResId());
                device.setDeviceType(group.getTypeStrId());
                device.setType(group.getModelHm());
                int install = 0;
                for (String type : group.getDevType()) {
                    install = DeviceDaoUtil.getInstance().isDevTypeExists(type);
                    if (install>0){
                        break;
                    }
                }
                float fraction = install > 0 ? group.getFraction() : 0.0f;
                mFraction = mFraction + fraction;
                device.setInstall(install);
                device.setFraction(fraction);
                deviceList2.add(device);
            }else if (group.getDeviceType() == CellsEnum.SMART_DEVICE_THEFT){
                AssessBean.DeviceBen device = new AssessBean.DeviceBen();
                device.setDeviceIcon(group.getDrawableResId());
                device.setDeviceType(group.getTypeStrId());
                int install = 0;
                for (String type : group.getDevType()) {
                    if ("IPC".equals(type)) {
                        install = DeviceDaoUtil.getInstance().findAllCamera().size();
                    }else {
                        install = DeviceDaoUtil.getInstance().isDevTypeExists(type);
                    }
                    if (install > 0) {
                        break;
                    }
                }
                float fraction = install > 0 ? group.getFraction() : 0.0f;
                mFraction = mFraction + fraction;
                device.setInstall(install);
                device.setFraction(fraction);
                deviceList3.add(device);
            }else if (group.getDeviceType() == CellsEnum.SMART_DEVICE_ENVIRONMENT){
                AssessBean.DeviceBen device = new AssessBean.DeviceBen();
                device.setDeviceIcon(group.getDrawableResId());
                device.setDeviceType(group.getTypeStrId());
                int install = 0;
                for (String type : group.getDevType()) {
                    install = DeviceDaoUtil.getInstance().isDevTypeExists(type);
                    if (install>0){
                        break;
                    }
                }
                float fraction = install > 0 ? group.getFraction() : 0.0f;
                mFraction = mFraction + fraction;
                device.setInstall(install);
                device.setFraction(fraction);
                deviceList4.add(device);
            }
        }
        AssessBean.DeviceBen fhSmoke = null;
        AssessBean.DeviceBen sxSmoke = null;
        AssessBean.DeviceBen thSmoke = null;
        AssessBean.DeviceBen oldGAS = null;
        AssessBean.DeviceBen newGAS = null;
        AssessBean.DeviceBen coAlarm = null;
        AssessBean.DeviceBen COAndGAS = null;

        for (AssessBean.DeviceBen deviceBen:deviceList2) {
            if ("GS559A".equals(deviceBen.getType())){
                fhSmoke = deviceBen;
            }else if ("GS530D".equals(deviceBen.getType())){
                sxSmoke = deviceBen;
            }else if (deviceBen.getType().contains("GS412")){
                thSmoke = deviceBen;
            }else if ("GS871A".equals(deviceBen.getType())){
                newGAS = deviceBen;
            }else if ("GS870W".equals(deviceBen.getType())){
                oldGAS = deviceBen;
            }else if ("GS816A".equals(deviceBen.getType())){
                coAlarm = deviceBen;
            }else if ("GS891A".equals(deviceBen.getType())){
                COAndGAS = deviceBen;
            }
        }

        if (fhSmoke !=null && fhSmoke.getInstall()>0){
            if (sxSmoke!=null && sxSmoke.getInstall()>0){
                mFraction = mFraction - sxSmoke.getFraction();
            }
            if (thSmoke!=null && thSmoke.getInstall()>0){
                mFraction = mFraction - thSmoke.getFraction();
            }
        }

        if (COAndGAS != null && COAndGAS.getInstall()>0){
            if (oldGAS!=null && oldGAS.getInstall()>0){
                mFraction = mFraction - oldGAS.getFraction();
            }
            if (newGAS!=null && newGAS.getInstall()>0){
                mFraction = mFraction - newGAS.getFraction();
            }
            if (coAlarm!=null && coAlarm.getInstall()>0){
                mFraction = mFraction - coAlarm.getFraction();
            }
        }

        if (newGAS != null && newGAS.getInstall()>0){
            if (oldGAS!=null && oldGAS.getInstall()>0){
                mFraction = mFraction - oldGAS.getFraction();
            }
        }
    }

    @Override
    public void getAssessData() {
        AssessBean mGateway = new AssessBean();
        AssessBean mFire = new AssessBean();
        AssessBean mTheft = new AssessBean();
        AssessBean mAmbient = new AssessBean();

        mGateway.setSmartIcon(R.mipmap.intelligent_system);
        mGateway.setSmartType(mContext.getString(R.string.ali_wifi_product));
        mGateway.setSmartDevice(deviceList1);

        mFire.setSmartIcon(R.mipmap.intelligent_fire_protection);
        mFire.setSmartType(mContext.getString(R.string.ali_alarm_product));
        mFire.setSmartDevice(deviceList2);

        mTheft.setSmartIcon(R.mipmap.device_prevent);
        mTheft.setSmartType(mContext.getString(R.string.ali_theft_product));
        mTheft.setSmartDevice(deviceList3);

        mAmbient.setSmartIcon(R.mipmap.intelligent_environment);
        mAmbient.setSmartType(mContext.getString(R.string.ali_environment_product));
        mAmbient.setSmartDevice(deviceList4);

        List<AssessBean> mList = new ArrayList<>();
        mList.add(mGateway);
        mList.add(mFire);
        mList.add(mTheft);
        mList.add(mAmbient);
        mView.showAssessInfo(mFraction, mList);
    }

    @Override
    public void getInstallData() {
        List<AssessBean.DeviceBen> deviceListAll = new ArrayList<>();
        deviceListAll.addAll(deviceList1);
        deviceListAll.addAll(deviceList2);
        deviceListAll.addAll(deviceList3);
        deviceListAll.addAll(deviceList4);
        mView.showInstallInfo(mFraction, deviceListAll);
    }

    @Override
    public void startAnalyze() {
        new Thread(this::getDeviceInstallInfo).start();
    }

}
